package com.briup.day34;

public class CommunicationPractice {
	
	static class PrintThread extends Thread{
		static Object lock = new Object();
		
		public PrintThread(String name) {
			super(name);
		
		}
		
		static int count = 0;
		@Override
		public void run() {
			for (int i = 0; i < 1000; i++) {
				System.out.println(getName()+":"+i);
				if(i == 500) {
					synchronized (lock) {
						try {
							if (count != 2) {
								System.out.println(getName()+ ++count+"到达500,开始等待...");
								lock.wait();
							}else {
								System.out.println();
								lock.notifyAll();
							}
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
			System.out.println(getName()+"打印结束!");
		}
	}
	
	public static void main(String[] args) {
		PrintThread t1 = new PrintThread("线程A");
		PrintThread t2 = new PrintThread("线程B");
		PrintThread t3 = new PrintThread("线程C");
		
		t1.start();
		t2.start();
		t3.start();
		
		try {
			t1.join();
			t2.join();
			t3.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("所有打印任务结束!");
	}

}
